package com.dream.dao.impl;

import com.dream.dao.SecurityDao;
import com.dream.domain.user.User;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: ZhongGang
 * Date: 12-12-23
 * Time: 上午1:01
 */
public class SecurityDaoImpl extends DomainDaoImpl implements SecurityDao {

    @Override
    public User findUserByUsername(String username) {
        final String hql = "from User u where u.token.username=? and u.active=true";
        return getHibernateTemplate().execute(new HibernateCallback<User>() {
            @Override
            public User doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createQuery(hql);
                return (User) query.uniqueResult();
            }
        });
    }
}
